<template>
  <div>
    <el-upload
      :action="BaseImageUrl"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :on-preview="handlePictureCardPreview"
      v-bind="elProps"
      v-model:file-list="fileList"
      class="avatar-uploader"
    >
      <!-- <img v-if="imageUrl ==1"  class="avatar" /> -->
      <el-icon class="avatar-uploader-icon"><Plus /></el-icon>
    </el-upload>
    <el-dialog v-model="dialogVisible" :close-on-click-modal="false">
      <img w-full :src="dialogImageUrl" alt="Preview Image" />
    </el-dialog>
  </div>
</template>

<script setup>
import { ref,watch } from "vue";
import { ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
import { BaseImageUrl } from "@/common/data.js";
const props = defineProps({
  elProps: Object,
  modelValue: [Array, String],
});

const fileList = ref([]);
fileList.value = props.modelValue;
watch(()=>props.modelValue,(newval)=>{
fileList.value = newval
})
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url
  dialogVisible.value = true
}

const emit = defineEmits(["update:modelValue"]);
const handleAvatarSuccess = (response, uploadFile) => {
  if (response.code == 200) {
    console.log(fileList.value);
      // fileList.value = fileList.value.map(({response, ...elseProp}) => ({url: response ? response.data : null, response, ...elseProp}))
    fileList.value.map((item) => {
      if (item.response) {
        item.url = item.response.data;
      }
    });
    emit("update:modelValue", fileList.value);
  }
  console.log(fileList.value);
};
const beforeAvatarUpload = (rawFile) => {
  
};
</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>